package fr.upmc.mdoc.tp1.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import fr.upmc.mdoc.tp1.domain.DAOContact;
import fr.upmc.mdoc.tp1.models.Contact;

/**
 * Servlet implementation class Modifier
 */
public class EditContact extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public EditContact() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		DAOContact dao = new DAOContact();

		int idContact = Integer.valueOf(request.getParameter("edit"));

		/*
		 * request.setAttribute("fn", request.getParameter("fn"));
		 * request.setAttribute("ln", request.getParameter("ln"));
		 * request.setAttribute("email", request.getParameter("email"));
		 */
		List<Contact> contacts = (List<Contact>) dao.getContacts();
		Contact contact = contacts.get(idContact);

		request.setAttribute("contactId", contact.getContactId());
		request.setAttribute("fn", contact.getFirstName());
		request.setAttribute("ln", contact.getLastName());
		request.setAttribute("email", contact.getEmail());
		request.setAttribute("street", contact.getAddress().getStreet());
		request.setAttribute("city", contact.getAddress().getCity());
		request.setAttribute("zip", contact.getAddress().getZip());
		request.setAttribute("country", contact.getAddress().getCountry());

		dao.editContat(idContact);

		RequestDispatcher rd = request
				.getRequestDispatcher("pages/editerContact.jsp");

		rd.forward(request, response);
	}
}
